﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using WPFServices.ViewModels.DomainViewModel.BomInfo;

namespace WPFServices.Views.DomainViews.BomInfo
{
    /// <summary>
    /// AddBom.xaml 的交互逻辑
    /// </summary>
    public partial class AddBomWindow : Window
    {
        public AddBomWindow()
        {
            InitializeComponent();
            DataContext = new AddBomViewModel();

            // 订阅ViewModel的关闭窗口事件
            if (DataContext is AddBomViewModel viewModel)
            {
                viewModel.CloseWindow += (result) =>
                {
                    try
                    {
                        DialogResult = result;
                        Close();
                    }
                    catch (Exception ex)
                    {
                        // 如果设置DialogResult失败，直接关闭窗口
                        System.Diagnostics.Debug.WriteLine($"关闭窗口时出错: {ex.Message}");
                        Close();
                    }
                };
                // 产品选择弹窗逻辑
                viewModel.OnProductSelected = (selected) =>
                {
                    var dlg = new WPFServices.Views.DomainViews.Productinfo.ProductViewWindow();
                    if (dlg.ShowDialog() == true)
                    {
                        var productVM = dlg.DataContext as WPFServices.ViewModels.DomainViewModel.Productinfo.ProductViewModel;
                        if (productVM != null && productVM.SelectedProduct != null)
                        {
                            viewModel.SelectedProduct = productVM.SelectedProduct;
                        }
                    }
                };
            }
        }
    }
}
